package com.dongdongshop.controller;

import com.dongdongshop.pojo.TbOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.vo.AddressVO;
import com.dongdongshop.vo.OrderVO;
import com.dongdongsop.data.AjaxResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @DubboReference
    private AddressService addressService;

    //跳转订单页
    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){
        return "home-index";
    }

    //添加订单
    @RequestMapping("submitOrder")
    @ResponseBody
    public AjaxResult submitOrder(Long id,String payType){
        try {
            TbOrder order = new TbOrder();
            AddressVO addressVO = addressService.getAddressVO(id);
            String receiverAreaName = addressVO.getProvince()+" "+addressVO.getCity()+" "+addressVO.getArea()+" "+addressVO.getAddress();
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            order.setReceiverAreaName(receiverAreaName);
            order.setPaymentType(payType);
            order.setUserId(user.getUsername());
            order.setReceiverMobile(addressVO.getMobile());
            order.setReceiver(addressVO.getContact());
            Map<String,String > map = orderService.submitOrder(order,user.getId());
            return AjaxResult.OK().setData(map);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    @RequestMapping("toHomeOrderSend")
    public String toHomeOrderSend(Model model){
        //获取登录用户的收货地址
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //通过userId获取所有商家订单
        List<OrderVO> orderVOList = orderService.getInfoByUserId(user.getUsername());
        model.addAttribute("orderVOList",orderVOList);
        System.out.println(orderVOList);
        //通过商家订单
        return "home-order-send";
    }
}
